package com.itany.netclass.service.impl;

import com.itany.netclass.constant.UserConstant;
import com.itany.netclass.dao.UserMapper;
import com.itany.netclass.entity.User;
import com.itany.netclass.entity.UserExample;
import com.itany.netclass.exception.*;
import com.itany.netclass.factory.ObjectFactory;
import com.itany.netclass.service.UserService;
import com.itany.netclass.util.MD5Util;
import com.itany.netclass.util.ParameterUtil;

import java.util.List;

/**
 * Author:sjy@itany.com
 * Date:21-10-12 下午5:13
 * Version:1.0-SNAPSHOT
 * Description:
 */
public class UserServiceImpl implements UserService {
    @Override
    public User login(String loginName, String password, String yzm , String code) throws NullSringException, CodeException, NameOrPasswordException, NoQuanXianException {
        if(ParameterUtil.isNull(loginName) || ParameterUtil.isNull(password) || ParameterUtil.isNull(yzm)){
            throw new NullSringException("请求参数异常");
        }
        System.out.println(yzm);
        System.out.println(code);
        if(!yzm.equals(code)){
            throw new CodeException("验证码错误");
        }
        UserMapper userMapper = (UserMapper) ObjectFactory.getObject("UserMapper");
        UserExample example=new UserExample();
        example.or()
                .andLoginNameEqualTo(loginName)
                .andStatusEqualTo(Integer.parseInt(UserConstant.USER_STATUS_QIYONG));
        List<User> users = userMapper.selectByExample(example);

        if(users.isEmpty()){
            throw new NameOrPasswordException("该帐号不存在或已被禁用");
        }
         User user=users.get(0);
        if(user.getRole().equals(UserConstant.USER_ROLE_PTUSER)){
            throw new NoQuanXianException("该用户没有权限，无法登录");
        }
        if(!MD5Util.md5(password).equals(user.getPassword())){
            throw new NameOrPasswordException("密码错误");
        }
        return user;
    }

    @Override
    public List<User> findAllUser(User user) throws NoLoginException {

        if(user==null){
            throw new NoLoginException("未登录，请先登录");
        }
        UserMapper userMapper = (UserMapper) ObjectFactory.getObject("UserMapper");
        UserExample example =new UserExample();
        List<User> list = userMapper.selectByExample(example);
//        for (User u:list) {
//            u.setSignDateStr(DateUtil.DateToString("yyyy-MM-dd hh:mm:ss",u.getSignDate()));
//            u.setCreateDateStr(DateUtil.DateToString("yyyy-MM-dd hh:mm:ss",u.getCreateDate()));
//        }
        return list;
    }

    @Override
    public User findUserById(String id) {
        UserMapper userMapper = (UserMapper) ObjectFactory.getObject("UserMapper");
        User user = userMapper.selectByPrimaryKey(Integer.parseInt(id));
        user.setPassword(MD5Util.md5(user.getPassword()));
        if(user.getRole().equals(UserConstant.USER_ROLE_GJGLY)){
            user.setRole("管理员");
        } else {
            user.setRole("用户");
        }
        return user;
    }

    @Override
    public void modifyUserBy(String id, String username, String roleName, String password, String email) throws NullSringException, TpyeException, EmailExistException {
        UserMapper userMapper = (UserMapper) ObjectFactory.getObject("UserMapper");
        if(ParameterUtil.isNull(username) || ParameterUtil.isNull(roleName) || ParameterUtil.isNull(email)){
            throw new NullSringException("请求参数异常");
        }
        if(!email.matches("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$")){
            throw new TpyeException("邮箱格式错误");
        }
        UserExample example = new UserExample();
        example.or()
                .andEmailEqualTo(email)
                .andIdNotEqualTo(Integer.parseInt(id));
        List<User> users = userMapper.selectByExample(example);
        if(!users.isEmpty()){
            throw new EmailExistException("该邮箱已被注册，请更换");
        }
        User user=new User();
        user.setId(Integer.parseInt(id));
        user.setNickname(username);
        user.setEmail(email);
        if(!ParameterUtil.isNull(password)){
            user.setPassword(password);
        }
        if(roleName.equals("1001") || roleName.equals("管理员")){
            user.setRole(UserConstant.USER_ROLE_GJGLY);
        } else if (roleName.equals("1002") || roleName.equals("用户")){
            user.setRole(UserConstant.USER_ROLE_PTUSER);
        }else {
            throw new TpyeException("身份格式错误");
        }

//        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public void modifyStatusById(String id, String status) {
        UserMapper userMapper = (UserMapper) ObjectFactory.getObject("UserMapper");
        User user = new  User();
        user.setStatus(Integer.parseInt(status));
        user.setId(Integer.parseInt(id));
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public List<User> sreachUserByForm(String username, String userNickname, String email, String roleName, String kaishitime, String jieshutime, String kaishitime1, String jieshutime1) throws NullSringException {
        UserMapper userMapper = (UserMapper) ObjectFactory.getObject("UserMapper");
        List<User> list= userMapper.selectUserByForm(username,userNickname,email,roleName,kaishitime,jieshutime,kaishitime1,kaishitime1);
        if(list.isEmpty()){
            throw new NullSringException("为查询到符合条件的用户");
        }
        return list;
    }

    @Override
    public void modifyUserBy2(String id, String username, String roleName, String password, String email) throws NullSringException, TpyeException, EmailExistException, ServiceException {
        UserMapper userMapper = (UserMapper) ObjectFactory.getObject("UserMapper");
        if(ParameterUtil.isNull(username) || ParameterUtil.isNull(roleName) || ParameterUtil.isNull(email)){
            throw new NullSringException("请求参数异常");
        }
        if(!email.matches("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$")){
            throw new TpyeException("邮箱格式错误");
        }
        UserExample example = new UserExample();
        example.or()
                .andEmailEqualTo(email)
                .andIdNotEqualTo(Integer.parseInt(id));
        List<User> users = userMapper.selectByExample(example);
        if(!users.isEmpty()){
            throw new EmailExistException("该邮箱已被注册，请更换");
        }
        User user=new User();
        user.setId(Integer.parseInt(id));
        user.setNickname(username);
        user.setEmail(email);
        if(!ParameterUtil.isNull(password)){
            user.setPassword(password);
        }
        if(roleName.equals("1001") || roleName.equals("管理员")){
            user.setRole(UserConstant.USER_ROLE_GJGLY);
        } else if (roleName.equals("1002") || roleName.equals("用户")){
            user.setRole(UserConstant.USER_ROLE_PTUSER);
        }else {
            throw new TpyeException("身份格式错误");
        }

        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public void jianchaUsername(String nickname) throws NullSringException {
        if(ParameterUtil.isNull(nickname)){
            throw new NullSringException("昵称不可为空");
        }
    }

    @Override
    public void jianchaRoleName(String roleName) throws TpyeException, NullSringException {
        if(ParameterUtil.isNull(roleName)){
            throw new NullSringException("请求参数异常");

        }
        if(roleName.equals("1001") || roleName.equals("管理员")){

        } else if (roleName.equals("1002") || roleName.equals("用户")){

        }else {
            throw new TpyeException("身份格式错误");
        }
    }

    @Override
    public void jianchaEmail(String email,String id) throws TpyeException, NullSringException, EmailExistException {
        if(ParameterUtil.isNull(email)){
            throw new NullSringException("请求参数异常");

        }
        if(!email.matches("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$")){
            throw new TpyeException("邮箱格式错误");
        }
        UserMapper userMapper = (UserMapper) ObjectFactory.getObject("UserMapper");
        UserExample example=new UserExample();
        example.or().andEmailEqualTo(email).andIdNotEqualTo(Integer.parseInt(id));
        List<User> list = userMapper.selectByExample(example);
        if(!list.isEmpty()){
            throw new EmailExistException("该邮箱已经被使用，请更换");
        }

    }
}
